package com.java110.mall.goods.bmo.mainCategory.impl;

import com.java110.dto.file.FileRelDto;
import com.java110.dto.mainCategory.MainCategoryDto;
import com.java110.dto.mainCategory.MainCategoryProductDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.mall.goods.bmo.mainCategory.IGetMainCategoryBMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.goods.IMainCategoryInnerServiceSMO;
import com.java110.intf.goods.IMainCategoryProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getMainCategoryBMOImpl")
public class GetMainCategoryBMOImpl implements IGetMainCategoryBMO {

    @Autowired
    private IMainCategoryInnerServiceSMO mainCategoryInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    @Autowired
    private IMainCategoryProductInnerServiceSMO mainCategoryProductInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    /**
     * @param mainCategoryDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(MainCategoryDto mainCategoryDto) {


        int count = mainCategoryInnerServiceSMOImpl.queryMainCategorysCount(mainCategoryDto);

        List<MainCategoryDto> mainCategoryDtos = null;
        if (count > 0) {
            mainCategoryDtos = mainCategoryInnerServiceSMOImpl.queryMainCategorys(mainCategoryDto);
        } else {
            mainCategoryDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) mainCategoryDto.getRow()), count, mainCategoryDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    @Override
    public ResponseEntity<String> queryMainCategoryAllGoods(MainCategoryDto mainCategoryDto) {

        MainCategoryProductDto mainCategoryProductDto = new MainCategoryProductDto();
        mainCategoryProductDto.setCategoryType(mainCategoryDto.getCategoryType());
        mainCategoryProductDto.setCommunityId(mainCategoryDto.getCommunityId());

        List<MainCategoryProductDto> mainCategoryProductDtos =
                mainCategoryProductInnerServiceSMOImpl.queryMainCategoryAllGoods(mainCategoryProductDto);
        freshProductDtos(mainCategoryProductDtos, mainCategoryDto);
        List<MainCategoryDto> mainCategoryDtos = generatorMainCategorys(mainCategoryProductDtos);
        List<MainCategoryProductDto> tmpMainCategoryProductDtos = null;
        for (MainCategoryDto tmpMainCategoryDto : mainCategoryDtos) {
            tmpMainCategoryProductDtos = new ArrayList<>();
            for (MainCategoryProductDto tmpMainCategoryProductDto : mainCategoryProductDtos) {
                if (tmpMainCategoryProductDto.getMainCategoryId().equals(tmpMainCategoryDto.getMainCategoryId())) {
                    tmpMainCategoryProductDtos.add(tmpMainCategoryProductDto);
                }
            }
            tmpMainCategoryDto.setMainCategoryProducts(tmpMainCategoryProductDtos);
        }

        return ResultVo.createResponseEntity(mainCategoryDtos);
    }


    @Override
    public ResponseEntity<String> queryMainCategoryAllServs(MainCategoryDto mainCategoryDto) {

        MainCategoryProductDto mainCategoryProductDto = new MainCategoryProductDto();
        mainCategoryProductDto.setCategoryType(mainCategoryDto.getCategoryType());
        mainCategoryProductDto.setCommunityId(mainCategoryDto.getCommunityId());
        List<MainCategoryProductDto> mainCategoryProductDtos =
                mainCategoryProductInnerServiceSMOImpl.queryMainCategoryAllServs(mainCategoryProductDto);
        if (null != mainCategoryProductDtos && mainCategoryProductDtos.size() > 0) {
            String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
            imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
            for (MainCategoryProductDto housekeepingServ : mainCategoryProductDtos) {
                //封面图
                housekeepingServ.setCoverPhoto(imgUrl + doGetRentingFile(housekeepingServ.getProductId(), FileRelDto.REL_TYPE_CD_GOODS_COVER));
            }
        }
        List<MainCategoryDto> mainCategoryDtos = generatorMainCategorys(mainCategoryProductDtos);
        List<MainCategoryProductDto> tmpMainCategoryProductDtos = null;
        for (MainCategoryDto tmpMainCategoryDto : mainCategoryDtos) {
            tmpMainCategoryProductDtos = new ArrayList<>();
            for (MainCategoryProductDto tmpMainCategoryProductDto : mainCategoryProductDtos) {
                if (tmpMainCategoryProductDto.getMainCategoryId().equals(tmpMainCategoryDto.getMainCategoryId())) {
                    tmpMainCategoryProductDtos.add(tmpMainCategoryProductDto);
                }
            }
            tmpMainCategoryDto.setMainCategoryProducts(tmpMainCategoryProductDtos);
        }
        return ResultVo.createResponseEntity(mainCategoryDtos);
    }


    /**
     * 保存房屋租赁 图片
     *
     * @param pId
     */
    private String doGetRentingFile(String pId, String fileTypeCd) {

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjId(pId);
        fileRelDto.setRelTypeCd(fileTypeCd);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        FileRelDto fileRel = null;
        if (null != fileRelDtos && fileRelDtos.size() > 0) {
            fileRel = fileRelDtos.get(0);
            return fileRel.getFileSaveName();
        }
        return "";
    }




    private List<MainCategoryDto> generatorMainCategorys(List<MainCategoryProductDto> mainCategoryProductDtos) {

        List<MainCategoryDto> mainCategoryDtos = new ArrayList<>();
        boolean exists = false;
        MainCategoryDto tmpMainCategoryDto = null;
        for (MainCategoryProductDto tmpMainCategoryProductDto : mainCategoryProductDtos) {
            exists = false;
            for (MainCategoryDto mainCategoryDto : mainCategoryDtos) {
                if (tmpMainCategoryProductDto.getMainCategoryId().equals(mainCategoryDto.getMainCategoryId())) {
                    exists = true;
                }
            }
            if (!exists) {
                tmpMainCategoryDto = new MainCategoryDto();
                tmpMainCategoryDto.setMainCategoryId(tmpMainCategoryProductDto.getMainCategoryId());
                tmpMainCategoryDto.setCategoryName(tmpMainCategoryProductDto.getCategoryName());
                mainCategoryDtos.add(tmpMainCategoryDto);
            }
        }

        return mainCategoryDtos;
    }

    private void freshProductDtos(List<MainCategoryProductDto> productDtos, MainCategoryDto productDto) {

        if (productDtos == null || productDtos.size() < 1) {
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (MainCategoryProductDto tmpProductDto : productDtos) {
            productIds.add(tmpProductDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (ProductDto tmpProductDto : productDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    continue;
                }
                if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                    url = tmpFileRelDto.getFileRealName();
                }

                tmpProductDto.setCoverPhoto(url);
            }
        }

        //刷入库存和销量
        ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setStoreId(productDtos.get(0).getStoreId());
        productSpecValueDto.setProductIds(productIds.toArray(new String[productIds.size()]));
        List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductStockAndSales(productSpecValueDto);
        for (ProductDto tmpProduct : productDtos) {
            for (ProductSpecValueDto tmpProdSpecValue : productSpecValueDtos) {
                if (tmpProdSpecValue.getProductId().equals(tmpProduct.getProductId())) {
                    tmpProduct.setSales(tmpProdSpecValue.getSales());
                    tmpProduct.setStock(tmpProdSpecValue.getStock());
                }
            }
        }
    }

}
